package com.tianzhu.service;

import java.io.IOException;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tianzhu.domain.Cart;
import com.tianzhu.repositories.CartRepo;

@Service
@Transactional
@Slf4j
public class CartService {
	
	@Autowired
	CartRepo cartRepo;
	
	public Cart getCartById(Long cartId){
		return cartRepo.findOne(cartId);
	}

	public void update(Cart cart){
    	cartRepo.save(cart);
    }
	
	
	public Cart validate(Long cartId) throws IOException{
        Cart cart = getCartById(cartId);
        if(cart == null || cart.getCartItems().size() == 0){
            throw new IOException(cartId + "");
        }

        update(cart);
        return cart;
    }

}
